package org.jeecg.modules.demo.hydrology.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.demo.hydrology.dto.DbscdssDto;
import org.jeecg.modules.demo.hydrology.entity.Dbscdss;
import org.jeecg.modules.demo.hydrology.mapper.DbscdssMapper;
import org.jeecg.modules.demo.hydrology.service.IDbscdssService;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.entity.SysDict;
import org.jeecg.modules.system.entity.SysDictItem;
import org.jeecg.modules.system.mapper.SysDepartMapper;
import org.jeecg.modules.system.mapper.SysDictItemMapper;
import org.jeecg.modules.system.mapper.SysDictMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 地表水实时
 * @Author: jeecg-boot
 * @Date:   2024-04-17
 * @Version: V1.0
 */
@Service
public class DbscdssServiceImpl extends ServiceImpl<DbscdssMapper, Dbscdss> implements IDbscdssService {
    @Autowired
    private DbscdssMapper dbscdssMapper;
    @Autowired
    private SysDepartMapper sysDepartMapper;
    @Autowired
    private SysDictMapper sysDictMapper;
    @Autowired
    private SysDictItemMapper sysDictItemMapper;
    /**
     * 地表水实时-分页列表查询
     * @param page
     * @param queryWrapper
     * @return
     */
    @Override
    public IPage<DbscdssDto> getList(Page<Dbscdss> page, QueryWrapper<Dbscdss> queryWrapper) {
        Page<Dbscdss> dbscdssPage = dbscdssMapper.selectPage(page, queryWrapper);
        List<DbscdssDto> collect = dbscdssPage.getRecords().stream().map(dbscdss -> {
            DbscdssDto dbscdssDto = new DbscdssDto();
            BeanUtils.copyProperties(dbscdss, dbscdssDto);
            QueryWrapper<SysDepart> sysDepartQueryWrapper = new QueryWrapper<>();
            sysDepartQueryWrapper.eq("org_code", dbscdss.getStationMine());
            SysDepart sysDepart = sysDepartMapper.selectOne(sysDepartQueryWrapper);
            if (sysDepart != null){
                dbscdssDto.setStationName(sysDepart.getDepartName());
            }
            QueryWrapper<SysDict> sysDictQueryWrapper = new QueryWrapper<>();
            sysDictQueryWrapper.eq("dict_code","point_status");
            SysDict sysDict = sysDictMapper.selectOne(sysDictQueryWrapper);
            if (sysDict != null){
                QueryWrapper<SysDictItem> sysDictItemQueryWrapper = new QueryWrapper<>();
                sysDictItemQueryWrapper.eq("dict_id",sysDict.getId()).eq("item_value",dbscdss.getStationStatus());
                SysDictItem sysDictItem = sysDictItemMapper.selectOne(sysDictItemQueryWrapper);
                if (sysDictItem != null) {
                    dbscdssDto.setPointStatusItemText(sysDictItem.getItemText());
                }
            }
            QueryWrapper<SysDict> sysDictQueryWrapper1 = new QueryWrapper<>();
            sysDictQueryWrapper1.eq("dict_code","sensor_type");
            SysDict sysDict1 = sysDictMapper.selectOne(sysDictQueryWrapper1);
            if (sysDict1 != null){
                QueryWrapper<SysDictItem> sysDictItemQueryWrapper1 = new QueryWrapper<>();
                sysDictItemQueryWrapper1.eq("dict_id",sysDict1.getId()).eq("item_value",dbscdss.getSensorType());
                SysDictItem sysDictItem1 = sysDictItemMapper.selectOne(sysDictItemQueryWrapper1);
                if (sysDictItem1 != null) {
                    dbscdssDto.setSensorTypeItemText(sysDictItem1.getItemText());
                }
            }
            return dbscdssDto;
        }).collect(Collectors.toList());
        IPage<DbscdssDto> dbscdssDtoIPage = new Page<>();
        dbscdssDtoIPage.setRecords(collect);
        dbscdssDtoIPage.setTotal(dbscdssPage.getTotal());
        dbscdssDtoIPage.setPages(page.getPages());
        dbscdssDtoIPage.setSize(page.getSize());
        return dbscdssDtoIPage;
    }
}
